﻿using M_Purchase.Api.Appliction.Command.AllocateCommand;
using M_Purchase.Domain;
using M_Purchase.Repository;
using M_Purchase.Repository.PurchaseInterface;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace M_Purchase.Api.Appliction.CommandHandlers.AllocateCommandHandler
{
    /// <summary>
    /// 调拨创建命令处理器
    /// </summary>
    public class CreatedAllocateCommandHandler : IRequestHandler<CreatedAllocateCommand, int>
    {
        /// <summary>
        /// 调拨仓储接口
        /// </summary>
        protected readonly IAllocateRepository allocateRepository;

        /// <summary>
        /// 数据库上下文接口
        /// </summary>
        protected readonly EFContext eFContext;

        /// <summary>
        /// 仓库-原料-调拨关联表仓储接口
        /// </summary>
        protected readonly IWIAllocateRepository wIAllocateRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="allocateRepository">调拨仓储接口</param>
        /// <param name="eFContext">数据库上下文接口</param>
        /// <param name="wIAllocateRepository">仓库-原料-调拨关联表仓储接口</param>
        public CreatedAllocateCommandHandler(IAllocateRepository allocateRepository, EFContext eFContext, IWIAllocateRepository wIAllocateRepository)
        {
            this.allocateRepository = allocateRepository;
            this.eFContext = eFContext;
            this.wIAllocateRepository = wIAllocateRepository;
        }

        public async Task<int> Handle(CreatedAllocateCommand request, CancellationToken cancellationToken)
        {
            var strategy = eFContext.Database.CreateExecutionStrategy();

            return await strategy.ExecuteAsync(async () =>
            {
                using (var transaction = eFContext.Database.BeginTransaction())
                {
                    try
                    {
                        //调拨信息 
                        Allocate allocate = new Allocate
                        {
                            AllocateNumber = request.AllocateNumber,
                            AllocateDate = request.AllocateDate,
                            AllocateNum = request.AllocateNum,
                            AllocatePerson = request.AllocatePerson,
                            AllocateReason = request.AllocateReason,
                            Notes = request.Notes,
                            Associate = request.Associate,
                            Transport = request.Transport,
                            Reviewer = request.Reviewer,
                            ApprovedDate = DateTime.Now,
                            AuditStatus = "待审核",
                            AllocateStatus = "待审核",
                            sub = request.sub,
                            sid = request.sid,
                            jti = request.jti,
                            CreatedBy = request.CreatedBy,
                            CreatedDate = DateTime.Now,
                        };

                        await allocateRepository.AddAsync(allocate);

                        // 新增仓库-原料-调拨关联表仓储明细
                        WIAllocate wIAllocate = new WIAllocate
                        {
                            AllocateId = allocate.AllocateId,
                            WarehouseId = request.WarehouseId,
                            IngredientId = request.IngredientId,
                            sub = request.sub,
                            sid = request.sid,
                            jti = request.jti,
                            CreatedBy = request.CreatedBy,
                            CreatedDate = DateTime.Now,
                        };

                        await wIAllocateRepository.AddAsync(wIAllocate);


                        // 提交事务
                        transaction.Commit();
                        return 1;
                    }
                    catch (Exception)
                    {
                        // 回滚事务
                        transaction.Rollback();

                        throw;
                    }
                }
            });
        }
    }
}